added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSAutomateExcel / Program.cs
blobf5f773937c982a890ee9affee6b6725f26345261
1 /****************************** Module Header ******************************\
2 * Module Name: Program.cs
3 * Project: CSAutomateExcel
4 * Copyright (c) Microsoft Corporation.
5 *
6 * The CSAutomateExcel example demonstrates how to use Visual C# codes to
7 * create a Microsoft Excel instance, create a workbook, fill data into a
8 * specific range, save the workbook, close the Microsoft Excel application
9 * and then clean up unmanaged COM resources.
11 * Office automation is based on Component Object Model (COM). When you call a
12 * COM object of Office from managed code, a Runtime Callable Wrapper (RCW) is
13 * automatically created. The RCW marshals calls between the .NET application
14 * and the COM object. The RCW keeps a reference count on the COM object. If
15 * all references have not been released on the RCW, the COM object of Office
16 * does not quit and may cause the Office application not to quit after your
17 * automation. In order to make sure that the Office application quits cleanly,
18 * the sample demonstrates two solutions.
20 * Solution1.AutomateExcel demonstrates automating Microsoft Excel application
21 * by using Microsoft Excel Primary Interop Assembly (PIA) and explicitly
22 * assigning each COM accessor object to a new varaible that you would
23 * explicitly call Marshal.FinalReleaseComObject to release it at the end.
25 * Solution2.AutomateExcel demonstrates automating Microsoft Excel application
26 * by using Microsoft Excel PIA and forcing a garbage collection as soon as
27 * the automation function is off the stack (at which point the RCW objects
28 * are no longer rooted) to clean up RCWs and release COM objects.
30 * This source is subject to the Microsoft Public License.
31 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
32 * All other rights reserved.
34 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
35 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
36 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
37 \***************************************************************************/
39 #region Using directives
40 using System;
41 #endregion
44 namespace CSAutomateExcel
46 class Program
48 [STAThread]
49 static void Main(string[] args)
51 // Solution1.AutomateExcel demonstrates automating Microsoft
52 // Excel application by using Microsoft Excel PIA and explicitly
53 // assigning each COM accessor object to a new varaible that you
54 // would explicitly call Marshal.FinalReleaseComObject to release
55 // it at the end.
56 Solution1.AutomateExcel();
58 Console.WriteLine();
60 // Solution2.AutomateExcel demonstrates automating Microsoft
61 // Excel application by using Microsoft Excel PIA and forcing a
62 // garbage collection as soon as the automation function is off
63 // the stack (at which point the RCW objects are no longer rooted)
64 // to clean up RCWs and release COM objects.
65 Solution2.AutomateExcel();